package jmine.tec.services.timer;

import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.execution.BaseServicesPageExecutor;
import jmine.tec.utils.debug.DebugRuntime;
import jmine.tec.utils.debug.TimerGroup;
import jmine.tec.utils.debug.impl.DebugRuntimeFactory;

/**
 * @author jrenaut
 */
@ServiceImplementor(action = ActionsEnum.EXECUTAR)
public abstract class AbstractTimerService {

    public static final String TIMER = "Timer";

    public static final String IDENTIFIER = "Identifier";

    private String timer;

    private String identifier;

    /**
     */
    @Execution
    public final void execute() {
        this.doExecute(this.getTimerGroup(), this.identifier);
    }

    /**
     * @param timerGroup a {@link TimerGroup}
     * @param readingIdentifier an identifier
     */
    protected abstract void doExecute(TimerGroup timerGroup, String readingIdentifier);

    /**
     * @return the {@link TimerGroup}
     */
    private TimerGroup getTimerGroup() {
        DebugRuntime runtime = DebugRuntimeFactory.getInstance();
        return runtime.getOrCreateTimer(this.timer == null ? BaseServicesPageExecutor.SERVICES_TIMER_IDENTIFIER : this.timer);
    }

    /**
     * @param timer the timer to set
     */
    @Input(fieldName = TIMER, required = false)
    public void setTimer(String timer) {
        this.timer = timer;
    }

    /**
     * @param identifier the identifier to set
     */
    @Input(fieldName = IDENTIFIER)
    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }

}
